package com.ruoyi.production.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.ProductManagementMapper;
import com.ruoyi.production.domain.ProductManagement;
import com.ruoyi.production.service.IProductManagementService;

/**
 * 产品列表Service业务层处理
 * 
 * @author admin
 * @date 2024-08-30
 */
@Service
public class ProductManagementServiceImpl implements IProductManagementService 
{
    @Autowired
    private ProductManagementMapper productManagementMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询产品列表
     * 
     * @param productId 产品列表主键
     * @return 产品列表
     */
    @Override
    public ProductManagement selectProductManagementByProductId(Long productId)
    {
        return productManagementMapper.selectProductManagementByProductId(productId);
    }

    /**
     * 查询产品列表列表
     * 
     * @param productManagement 产品列表
     * @return 产品列表
     */
    @Override
    public List<ProductManagement> selectProductManagementList(ProductManagement productManagement)
    {
        return productManagementMapper.selectProductManagementList(productManagement);
    }


    /**
     * 批量新增产品列
     *
     * @param productManagements 产品列List
     * @return 结果
     */
    @Override
    public int batchInsertProductManagement(List<ProductManagement> productManagements)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(productManagements)) {
            try {
                for (int i = 0; i < productManagements.size(); i++) {
                    int row = productManagementMapper.insertProductManagement(productManagements.get(i));
                    // 防止内存溢出，没100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%3 == 0) || i == productManagements.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 新增产品列表
     * 
     * @param productManagement 产品列表
     * @return 结果
     */
    @Override
    public int insertProductManagement(ProductManagement productManagement)
    {
        productManagement.setCreateTime(DateUtils.getNowDate());
        return productManagementMapper.insertProductManagement(productManagement);
    }

    /**
     * 修改产品列表
     * 
     * @param productManagement 产品列表
     * @return 结果
     */
    @Override
    public int updateProductManagement(ProductManagement productManagement)
    {
        return productManagementMapper.updateProductManagement(productManagement);
    }

    /**
     * 批量删除产品列表
     * 
     * @param productIds 需要删除的产品列表主键
     * @return 结果
     */
    @Override
    public int deleteProductManagementByProductIds(Long[] productIds)
    {
        return productManagementMapper.deleteProductManagementByProductIds(productIds);
    }

    /**
     * 删除产品列表信息
     * 
     * @param productId 产品列表主键
     * @return 结果
     */
    @Override
    public int deleteProductManagementByProductId(Long productId)
    {
        return productManagementMapper.deleteProductManagementByProductId(productId);
    }
}
